package com.samphanie.auiu.auth.validation.code.captcha.Image;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.samphanie.auiu.auth.validation.code.captcha.Captcha;
import com.samphanie.auiu.auth.validation.code.captcha.service.AbstractValidateCodeProcessor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;

/**
 * 生成图形验证码
 *
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Slf4j
@RequiredArgsConstructor
@Component("imageCaptcha")
public class ImageCodeProcessor extends AbstractValidateCodeProcessor<Captcha> {

    private final DefaultKaptcha kaptcha;

    /**
     * 发送校验码
     */
    @Override
    protected void send(ServletWebRequest request, Captcha captcha) throws Exception {
        if (request.getResponse() != null) {
            ImageIO.write(kaptcha.createImage(captcha.getCode()), "JPEG", request.getResponse().getOutputStream());
        }
    }
}
